using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The EmployeeSalaryTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeSalaryTable objects.
/// </summary>
public class EmployeeSalaryTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeSalaryTable from the underlying datasource.
/// </summary>
/// <param name="employeeSalaryKey">The unique EmployeeSalaryKey of the EmployeeSalaryTable in the database.</param>
/// <returns>An EmployeeSalaryTable when the EmployeeSalaryKey was found in the database, or null otherwise.</returns>
public static EmployeeSalaryTable GetItem(int employeeSalaryKey)
{
EmployeeSalaryTable myEmployeeSalaryTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeSalaryKey", employeeSalaryKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeSalaryTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeSalaryTable;
}
}

/// <summary>
/// Returns a list with EmployeeSalaryTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTable objects.</returns>
public static EmployeeSalaryTableList GetList()
{
EmployeeSalaryTableList tempList = new EmployeeSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeSalaryTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTable objects.</returns>
public static EmployeeSalaryTableList GetListByCurrencyCode(short CurrencyCode)
{
EmployeeSalaryTableList tempList = new EmployeeSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableSelectListByCurrencyCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@currencyCode", CurrencyCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EmployeeSalaryTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTable objects.</returns>
public static EmployeeSalaryTableList GetListByPaymentTypeCode(short PaymentTypeCode)
{
EmployeeSalaryTableList tempList = new EmployeeSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableSelectListByPaymentTypeCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@paymentTypeCode", PaymentTypeCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EmployeeSalaryTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeSalaryTable objects.</returns>
public static EmployeeSalaryTableList GetListByUserAccountKey(int UserAccountKey)
{
EmployeeSalaryTableList tempList = new EmployeeSalaryTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a EmployeeSalaryTable in the database.
/// </summary>
/// <param name="myEmployeeSalaryTable">The EmployeeSalaryTable instance to save.</param>
/// <returns>The new EmployeeSalaryKey if the EmployeeSalaryTable is new in the database or the existing EmployeeSalaryKey when an item was updated.</returns>
public static int Save(EmployeeSalaryTable myEmployeeSalaryTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeSalaryTable.EmployeeSalaryKey == -1)
{myCommand.Parameters.AddWithValue("@employeeSalaryKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeSalaryKey", myEmployeeSalaryTable.EmployeeSalaryKey);
}
if (myEmployeeSalaryTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeSalaryTable.UserAccountKey);
}
if (myEmployeeSalaryTable.PaymentTypeCode == null){
myCommand.Parameters.AddWithValue("@paymentTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@paymentTypeCode", myEmployeeSalaryTable.PaymentTypeCode);
}
if (myEmployeeSalaryTable.Amount == null){
myCommand.Parameters.AddWithValue("@amount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@amount", myEmployeeSalaryTable.Amount);
}
if (myEmployeeSalaryTable.Allotment1 == null){
myCommand.Parameters.AddWithValue("@allotment1", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@allotment1", myEmployeeSalaryTable.Allotment1);
}
if (myEmployeeSalaryTable.Allotment2 == null){
myCommand.Parameters.AddWithValue("@allotment2", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@allotment2", myEmployeeSalaryTable.Allotment2);
}
if (myEmployeeSalaryTable.CurrencyCode == null){
myCommand.Parameters.AddWithValue("@currencyCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@currencyCode", myEmployeeSalaryTable.CurrencyCode);
}
if (myEmployeeSalaryTable.StartDate == null){
myCommand.Parameters.AddWithValue("@startDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@startDate", myEmployeeSalaryTable.StartDate);
}
if (myEmployeeSalaryTable.EndDate == null){
myCommand.Parameters.AddWithValue("@endDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@endDate", myEmployeeSalaryTable.EndDate);
}
if (myEmployeeSalaryTable.Remarks == null){
myCommand.Parameters.AddWithValue("@remarks", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remarks", myEmployeeSalaryTable.Remarks);
}
if (myEmployeeSalaryTable.Active == null){
myCommand.Parameters.AddWithValue("@active", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@active", myEmployeeSalaryTable.Active);
}
if (myEmployeeSalaryTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeSalaryTable.Status);
}
if (myEmployeeSalaryTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeSalaryTable.CreatedDateTime);
}
if (myEmployeeSalaryTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeSalaryTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeSalaryTable from the database.
/// </summary>
/// <param name="employeeSalaryKey">The EmployeeSalaryKey of the EmployeeSalaryTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int employeeSalaryKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeSalaryTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeSalaryKey", employeeSalaryKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeSalaryTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeSalaryTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeSalaryTable myEmployeeSalaryTable = new EmployeeSalaryTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeSalaryKey")))
{
    myEmployeeSalaryTable.EmployeeSalaryKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeSalaryKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeSalaryTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentTypeCode")))
{
myEmployeeSalaryTable.PaymentTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Amount")))
{
myEmployeeSalaryTable.Amount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Amount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Allotment1")))
{
myEmployeeSalaryTable.Allotment1 = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Allotment1"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Allotment2")))
{
myEmployeeSalaryTable.Allotment2 = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Allotment2"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyCode")))
{
myEmployeeSalaryTable.CurrencyCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CurrencyCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
{
myEmployeeSalaryTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
{
myEmployeeSalaryTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remarks")))
{
myEmployeeSalaryTable.Remarks = myDataRecord.GetString(myDataRecord.GetOrdinal("Remarks"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Active")))
{
myEmployeeSalaryTable.Active = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Active"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeSalaryTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myEmployeeSalaryTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myEmployeeSalaryTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEmployeeSalaryTable;
}
}
